//
//  SAPPlayerViewController.m
//  CarsHome
//
//  Created by dapeng on 15/12/16.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPPlayerViewController.h"
#import "SAPAudioOneModel.h"
#import "SAPAudioTwoModel.h"
@interface SAPPlayerViewController ()
@property (nonatomic, retain) SAPAudioOneModel *oneModel;
@end

@implementation SAPPlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.player = [[STKAudioPlayer alloc] initWithOptions:(STKAudioPlayerOptions){ .flushQueueOnSeek = YES, .enableVolumeMixer = YES, .equalizerBandFrequencies = {50, 100, 200, 400, 800, 1600, 2600, 16000} }];
    self.backMusicImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
    [self.view addSubview:self.backMusicImage];
    [_backMusicImage release];
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0.9;
    [self.backMusicImage addSubview:backView];
    [backView release];
    
    self.musicImage = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_SIZE.width - SCREEN_SIZE.width + 200) / 2, SCREEN_SIZE.height / 2 - SCREEN_SIZE.width + 200, SCREEN_SIZE.width - 200, SCREEN_SIZE.width - 200)];
    self.musicImage.layer.cornerRadius = (SCREEN_SIZE.width - 200) / 2;
    self.musicImage.layer.masksToBounds = YES;
    [self.view addSubview:self.musicImage];
    [_musicImage release];
    self.titleName = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_SIZE.width - SCREEN_SIZE.width + 200) / 2, SCREEN_SIZE.height - 250, SCREEN_SIZE.width - 200, 30)];
    self.titleName.text = self.titles;
    self.titleName.textColor = [UIColor whiteColor];
    self.titleName.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.titleName];
    [_titleName release];
    [self playButtons];
    [self nextButtons];
    [self previousTracks];
    [self playMusic];
    [self progressSliders];
    [self startAnimation];

    
}

- (void)playButtons {
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playButton.frame = CGRectMake((SCREEN_SIZE.width - 50) / 2, SCREEN_SIZE.height - 200, 50, 50);
    [self.playButton setImage:[UIImage imageNamed:@"PauseIcon"] forState:UIControlStateNormal];
    [self.playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.playButton.layer.cornerRadius = 25;
    
    [self.view addSubview:self.playButton];
}
- (void)nextButtons {
    self.nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextButton.frame = CGRectMake((SCREEN_SIZE.width - 50) / 2 - 100, SCREEN_SIZE.height - 200, 50, 50);
    [self.nextButton setImage:[UIImage imageNamed:@"PreviousPlayer"] forState:UIControlStateNormal];
    [self.nextButton addTarget:self action:@selector(previousTracksAction:) forControlEvents:UIControlEventTouchUpInside];
    [self playMusic];
    
    [self.view addSubview:self.nextButton];
}
- (void)previousTracks {
    self.previousTrack = [UIButton buttonWithType:UIButtonTypeCustom];
    self.previousTrack.frame = CGRectMake((SCREEN_SIZE.width - 50) / 2 + 100, SCREEN_SIZE.height - 200, 50, 50);
    [self.previousTrack setImage:[UIImage imageNamed:@"nextPlayer"] forState:UIControlStateNormal];
    [self.previousTrack addTarget:self action:@selector(nextButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self playMusic];
    [self.view addSubview:self.previousTrack];
}

- (void)progressSliders {
    
    self.progressSlider = [[UISlider alloc] initWithFrame:CGRectMake(10, SCREEN_SIZE.height - 100, SCREEN_SIZE.width - 20, 20)];
    [self.progressSlider addTarget:self action:@selector(progressSliderAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.progressSlider];
    [_progressSlider release];
}



- (void)playButtonAction:(UIButton *)play {
    
    if (self.player.state == STKAudioPlayerStatePaused) {
        [self.playButton setImage:[UIImage imageNamed:@"PauseIcon"] forState:UIControlStateNormal];
        [self.player resume];
    }else if (self.player.state == STKAudioPlayerStatePlaying) {
        [self.playButton setImage:[UIImage imageNamed:@"playerIcon"] forState:UIControlStateNormal];
        [self.player pause];
    }else {
        [self playMusic];
    }
}

- (void)nextButtonAction:(UIButton *)next {
    self.indexRow++;

    if (self.indexRow == self.allMusicArray.count) {
        self.indexRow = 0;
    }
    SAPAudioTwoModel *twoModel = [self.allMusicArray objectAtIndex:self.indexRow];
    self.string = twoModel.program_fileurl;
    self.titleName.text = twoModel.program_title;
    [self playMusic];
}
- (void)previousTracksAction:(UIButton *)previous {
    self.indexRow--;

    if (self.indexRow == -1) {
        self.indexRow = self.allMusicArray.count - 1;
    }
    
    SAPAudioTwoModel *twoModel = [self.allMusicArray objectAtIndex:self.indexRow];
    self.titleName.text = twoModel.program_title;
    self.string = twoModel.program_fileurl;
    [self playMusic];
}

- (void)progressSliderAction:(UISlider *)slider {
    if (self.player) {
        
        [self.player seekToTime:slider.value];
        
    }
}

+(instancetype)sharePlayerViewController
{
    
    static SAPPlayerViewController *player = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        player = [[SAPPlayerViewController alloc] init];
        
    });
    
    return player;
}
- (void)setString:(NSString *)string {
    if (_string != string) {
        [_string release];
        _string = [string copy];
    }
    
    [self playMusic];

}

- (void)playMusic {
        [self.player play:self.string];
    NSString *str = [self.picture stringByReplacingOccurrencesOfString:@"200x0_0_" withString:@""];
    [self.backMusicImage sd_setImageWithURL:[NSURL URLWithString:str]];
    [self.musicImage sd_setImageWithURL:[NSURL URLWithString:str]];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(track) userInfo:nil repeats:YES];
}
- (void)startAnimation
{
    CGAffineTransform endAngle = CGAffineTransformMakeRotation(_angle * (M_PI / 180.0f));
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.musicImage.transform = endAngle;
    } completion:^(BOOL finished) {
        _angle += 10;
        [self startAnimation];
    }];
}


-(void)track {
    
    self.progressSlider.maximumValue = self.player.duration;//音乐总共时长
    self.progressSlider.value = self.player.progress;//当前进度
    
    //当前时长进度progress
    NSInteger proMin = (NSInteger)self.player.progress / 60;//当前秒
    NSInteger proSec = (NSInteger)self.player.progress % 60;//当前分钟
    
    //duration 总时长
    NSInteger durMin = (NSInteger)self.player.duration / 60;//总秒
    NSInteger durSec = (NSInteger)self.player.duration % 60;//总分钟
    
    self.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld / %02ld:%02ld", proMin, proSec, durMin, durSec];
}

- (void)dealloc {
//    [_allMusicArray release];
    [_musicImage release];
    [super dealloc];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
